Public Class wxPlayer

    Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer 'Get the error message of the mcidevice if any
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer 'Send command strings to the mci device
    ''Private Declare Function getTickCount Lib "kernel32" Alias "GetTickCount" () As Integer

    Private WithEvents mTimer As New Timer
    Private retString As String = Space(128)
    Private mDuration As Int32 = 0

    Public Event Player_TimerTick() '(ByVal mPosition As Int32)
    Public Event Player_Stopped()
    Public Event Player_Paused()


    Public Sub New()
        ''
    End Sub


    Public Function OpenFile(ByVal Filename As String) As Boolean
        Try
            If Not System.IO.File.Exists(Filename) Then
                ''MessageBox.Show("File not found!", "WinXound Player OpenFile Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
                Exit Function
            End If

            Filename = """" & Filename & """"

            mciSendString("close mysound", 0, 0, 0)
            mciSendString("open " & Filename & " type waveaudio alias mysound", 0, 0, 0)

            mciSendString("seek mysound to end", 0, 0, 0)
            mDuration = Me.Position
            mciSendString("seek mysound to start", 0, 0, 0)

            Return True

        Catch ex As Exception
            Return False
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try

    End Function

    Public Sub StopSound()
        Try
            mciSendString("stop mysound", 0, 0, 0)
            mciSendString("seek mysound to start", 0, 0, 0)
            StopTimer()
            RaiseEvent Player_Stopped()

        Catch ex As Exception
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try
    End Sub

    Public Sub PauseSound()
        Try
            mciSendString("pause mysound", 0, 0, 0)
            StopTimer()
            RaiseEvent Player_Paused()

        Catch ex As Exception
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try
    End Sub

    Public Sub PlaySound()
        Try
            ''mciSendString("seek mysound to start", 0, 0, 0)
            mciSendString("play mysound", 0, 0, 0)
            StartTimer()

        Catch ex As Exception
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try
    End Sub

    Public Function IsPlaying() As Boolean
        Try
            mciSendString("status mysound mode", retString, 128, 0)

            retString = Left(retString, 7)

            Return (retString = "playing")

        Catch ex As Exception
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try

    End Function

    'Public Function Status() As String
    '    Try
    '        mciSendString("status mysound mode", retString, 128, 0)

    '        retString = Left(retString, 7)

    '        Return retString

    '    Catch ex As Exception
    '        MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
    '    End Try

    'End Function

    Public ReadOnly Property Duration() As Int32
        Get
            Return mDuration
        End Get
    End Property

    Public Property Position() As Int32

        Get
            Try
                mciSendString("set mysound time format ms", 0, 0, 0)
                mciSendString("status mysound position", retString, 128, 0)
                Return Val(retString)
            Catch ex As Exception
                MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
            End Try
        End Get

        Set(ByVal Value As Int32)
            Try
                mciSendString("set movie time format ms", 0, 0, 0)
                If Me.IsPlaying() = True Then
                    mciSendString("play mysound from " & Value, 0, 0, 0)
                Else
                    mciSendString("seek mysound to " & Value, 0, 0, 0)
                End If

            Catch ex As Exception
                MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
            End Try
        End Set

    End Property

    Public Sub Dispose()
        Try
            mciSendString("close all", 0, 0, 0)
        Catch ex As Exception
            MsgBox(ex.Message, " Error", MessageBoxButtons.OK)
        End Try
    End Sub


    '???????
    'Protected Overrides Sub Finalize()
    '    Me.Dispose()
    '    MyBase.Finalize()
    'End Sub



    '''''''''''''
    'TIMER STUFFS
    Private Sub StartTimer()
        mTimer.Enabled = True
        mTimer.Interval = 100
        mTimer.Start()
    End Sub

    Private Sub StopTimer()
        mTimer.Stop()
    End Sub

    Private Sub mTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mTimer.Tick
        If Me.Position >= Me.Duration Then
            Me.StopSound()
        End If
        If Me.IsPlaying = False Then
            mTimer.Stop()
        End If
        'Debug.WriteLine(Me.Status)
        RaiseEvent Player_TimerTick()
    End Sub

End Class
